package com.xnx3.j2ee.pluginManage;

import com.xnx3.j2ee.Global;
import com.xnx3.j2ee.util.SpringUtil;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;

/**
 * 项目启动后，自动开启插件扫描
 */
@Component
class PluginScan{

    public PluginScan() throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
        new Thread(new Runnable() {
            public void run() {
                while(SpringUtil.getApplicationContext() == null){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //当 SpringUtil 被Spring 加载后才会执行


                while(Global.system.size() == 0) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                //当 system 表数据加载后，也就是数据库初始化完毕后
                try {
                    PluginManage.scanPluginClass();
                } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException
                        | IllegalArgumentException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}